<template>
  <upload-excel :on-success="handleSuccess" />
</template>

<script>
import uploadExcel from '@/components/UploadExcel/index.vue'
import { importEmployeeAPI } from '@/api/employees'
import { formatDate } from '@/utils/index'
export default {
  name: 'Import',
  components: {
    uploadExcel
  },

  methods: {
    transExcle(results) {
      const mapInfo = {
        入职日期: 'timeOfEntry',
        手机号: 'mobile',
        姓名: 'username',
        转正日期: 'correctionTime',
        工号: 'workNumber',
        部门: 'departmentName',
        聘用形式: 'formOfEmployment'
      }
      return results.map(zhObj => {
        const enObj = {}
        console.log(Object.keys(zhObj))
        Object.keys(zhObj).forEach(zhKey => {
          if (mapInfo[zhKey] === 'timeOfEntry' || mapInfo[zhKey] === 'correctionTime') {
            enObj[mapInfo[zhKey]] = new Date(formatDate(zhObj[zhKey]))
          } else {
            enObj[mapInfo[zhKey]] = zhObj[zhKey]
          }
        })
        return enObj
      })
    },
    async handleSuccess({ header, results }) {
      console.log(header, results)
      const data = this.transExcle(results)
      const res = await importEmployeeAPI(data)
      this.$message.success('导入成功')
      this.$router.back()
      console.log(res)
    }
  }
}
</script>
